package com.studay;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.studay.bean.ChannelInfo;
import com.studay.bean.Const;
import com.studay.bean.Course;
import com.util.LogManager;

/**
 * 显示课程列表
 * 
 * @author LY
 * 
 */
public class CrourseActivity extends Activity {
	private ListView videoList;
	private List<Map<String, String>> video;
	private ChannelInfo channelInfo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.crouse);

		View mainLayout = findViewById(R.id.mainLayout);
		mainLayout.getBackground().setAlpha(180);

		Bundle bundle = this.getIntent().getBundleExtra("courseInfo");
		channelInfo = (ChannelInfo) bundle.getSerializable("channelInfo");

		TextView crouseInfoTextView = (TextView) findViewById(R.id.crouseInfoTextView);
		crouseInfoTextView.setText(bundle.getString("remark"));

		TextView titleTextView = (TextView) findViewById(R.id.titleTextView);
		titleTextView.setText(bundle.getString("name"));

		videoList = (ListView) findViewById(R.id.videoList);
		video = new ArrayList<Map<String, String>>();

		ArrayList<Course> childList = bundle
				.getParcelableArrayList("courseList");
		for (Course course : childList) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("data", "课程名：" + course.getName());
			map.put("url", course.getFilehash());
			video.add(map);
		}

		ListViewAdapter mySimpleAdapter = new ListViewAdapter(this,
				video,// 数据源
				R.layout.listviewvideo,// ListView内部数据展示形式的布局文件listitem.xml
				new String[] { "data", "url" },
				new int[] { R.id.listViewtext1 }// HashMap中的两个key值
		);

		videoList.setAdapter(mySimpleAdapter);
	}

	public class ListViewAdapter extends BaseAdapter {

		private LayoutInflater mInflater;
		private List<Map<String, String>> list;
		private int layoutID;

		public final class ViewHolder {
			public TextView info;
			public Button viewBtn;
		}

		public ListViewAdapter(Context context, List<Map<String, String>> list,
				int layoutID, String flag[], int ItemIDs[]) {
			this.mInflater = LayoutInflater.from(context);
			this.list = list;
			this.layoutID = layoutID;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return 0;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(layoutID, null);
				holder.info = (TextView) convertView
						.findViewById(R.id.textViewVideo);
				holder.viewBtn = (Button) convertView
						.findViewById(R.id.btnVideo);
				convertView.setTag(holder);

			} else {

				holder = (ViewHolder) convertView.getTag();
			}
			final int i = position;
			holder.info.setText((String) list.get(position).get("data"));
			holder.viewBtn.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					LogManager.LogShow(Const.TAG, "播放视频", LogManager.INFO);
					Intent intent = new Intent();
					Bundle bundle = new Bundle();
					if (null != channelInfo) {
						LogManager.LogShow(Const.TAG, "自动拼装URL", LogManager.INFO);
						bundle.putString("fileName", channelInfo.getProtocl()
								.toLowerCase()
								+ "://"
								+ channelInfo.getFisserverurl()
								+ "/"
								+ channelInfo.getNodes()
								+ "/"
								+ list.get(i).get("url"));
					} else {
						bundle.putString("fileName",
								"rtmp://121.52.214.197/vod/"
										+ list.get(i).get("url"));
					}
					intent.setClass(getApplicationContext(),
							FlashActivity.class);
					intent.putExtra("videoBundle", bundle);
					startActivity(intent);
				}
			});
			return convertView;
		}
	}
}
